from django import template
from systems.models import MenuButton, Role

register = template.Library()


@register.filter()
def role_switch(value):
    """翻译角色编码"""
    if value == 0:
        return "普通用户"
    elif value == 1:
        return "管理员"


@register.filter()
def has_permission(request, key):
    """是否拥有某按钮权限"""
    if request.user.is_superuser == 1:
        return True
    role_button = Role.objects.filter(userprofile=request.user, menu__web_path=request.path, permission__key=key)
    if role_button:
        return True
    else:
        return False
